<?php

namespace app\middleware;

use Closure;

//use app\libs\Auth;
use app\facade\Auth;
use think\facade\Db;
use think\facade\Cache;

class AfterMiddleware
{
    public function handle($request, Closure $next)
    {
        if (Auth::guard('admin')) {
            // 设置语言

            $this->getAccess();
        }

        return $next($request);
    }

    private function getAccess()
    {
        $auth = Auth::guard('admin');
        $session_admin_id = $auth['id'];

        if (!Cache::get('admin_access')) {
            $roles = Db::name('admin_role_user')
                ->alias('a')
                ->join('admin_roles b', 'b.id = a.role_id')
                ->where('a.admin_id', $session_admin_id)
                ->field('b.access')
                ->find();

            $access = $roles['access'];

            Cache::set('admin_access', $access, 3600 * 6);
        }
    }
}